const ora = require('ora')
// 挂起函数
async function sleep(n) {
    return new Promise((resolve,reject) => {
        setTimeout(resolve,n)
    })
}

// loading + 失败重新发起fetch
async function wrapLoading(fn,message,...args) {
    const spinner = ora(message);
    spinner.start(); // 开始加载

    try {
        let repos = await fn(args);
        spinner.succeed(); // 成功
        return repos;
    } catch (error) {
        spinner.fail('request failed, refetch...')
        await sleep(1000);
        console.log(error);
        return wrapLoading(fn,message,...args);
    }
    
    
}


module.exports = {
    wrapLoading,
    sleep
}